//The track system of Shared Takeaway Box Project 
/**************************************************************************
 
    Copyright (C) 2021  Flactis

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	To contact with Flactis, please see <https://gitee.com/sunkenstar>.

***************************************************************************/

#include <Wire.h>
#include <DFRobot_LIS2DH12.h>

//Pin Definition
static const int beep=8;//The Beep uses pin 8;
static const int AmSCL=18;//The Accelerometer's SCL uses pin 18;
static const int AmSDA=19;//The Accelerometer's SDA uses pin 19;

//Misc Variants
//If in 10 seconds more than 10 data is not zero then alarm.
//1 sec gen 2 data, thus 10 secs gen 20 data.
static int times;

//Physics Variants
static int16_t ax,ay,z;

//Accelerometer Initialization
DFRobot_LIS2DH12 Am; 

//Preparation
void setup() 
{
    Wire.begin(19,18);
    pinMode(beep,OUTPUT);
    Serial.begin(115200);
    while (Am.init(LIS2DH12_RANGE_16GA) == -1)
        Serial.println("No I2C devices found");
}

//Main Loop
void loop()
{
    times=0;
    for(int i=1;i<=10;i++)
    {
        delay(1000);
        Am.readXYZ(ax,ay,z);
        Am.mgScale(ax,ay,z);
        Serial.println(ax);
        Serial.println(ay);
        if(ax>100||ax<-100) times++;
        if(ay>100||ay<-100) times++;
        if(times>10)
        {
            for(int j=1;j<=10;j++)
            {
                digitalWrite(beep,HIGH);
                delay(300);
                digitalWrite(beep,LOW);
                delay(300);
            }
            break;
        }
    }
}